/**
    @project {
        "templateId": "org.helgoboss.custom",
        "version": "0.1-SNAPSHOT",
        "dependencies": [
            "org.scala-lang:scala-library:2.9.0-1",
            "org.helgoboss:launchlet-api:0.1-SNAPSHOT",
            "org.helgoboss:scala-additions:0.1-SNAPSHOT",
            "org.helgoboss:osgi-additions:0.1-SNAPSHOT",
            "org.helgoboss:commandlet-api:0.1-SNAPSHOT"
        ]
    }
*/

package org.helgoboss.start_menu_to_launchlets

import org.helgoboss.launchlet.Launchlet
import org.helgoboss.commandlet.ExecutionContext
import org.helgoboss.osgi_additions._
import org.helgoboss.osgi_additions.metatype._
import org.helgoboss.scala_additions.Implicits._
import org.helgoboss.scala_additions.RichFile
import java.io.File
import java.awt.Desktop


class Activator extends OsgiProvider with OsgiServiceWatcher with OsgiLogging {
    whenBundleActive {
        def indexDir(dir: File) {
            dir.tree.filter(_.file.getName.endsWith(".lnk")).foreach { node =>
                new Launchlet {
                    val fileName = node.path.components.last
                    val entryName = fileName.substring(0, fileName.size - 4)
                    val entryPath = node.path.components.dropRight(1) ++ List(entryName)
                    
                    val name = entryPath.mkString(" - ")
                    
                    def execute(executionContext: Option[ExecutionContext]) = {
                        Desktop.getDesktop.open(node.file)
                        0
                    }
                }.providesService[Launchlet]
            }
        }
        val userStartMenuDir = RichFile.userHomeDir / "Startmenü/Programme"
        val commonStartMenuDir = RichFile.userHomeDir.getParentFile / "All Users/Startmenü/Programme"
        indexDir(userStartMenuDir)
        indexDir(commonStartMenuDir)
    }
}